package com.roy.nio;

import java.io.*;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
import java.nio.charset.Charset;
import java.nio.file.OpenOption;
import java.nio.file.StandardOpenOption;

/**
 * @author ：楼兰
 * @date ：Created in 2021/6/3
 * @description:
 **/

public class FileChannelDemo1 {
    public static void main(String[] args) throws IOException {

        String message = "Netty Message";
        File sourceFile = new File("D://ft1.txt");
        if(sourceFile.exists()){
            sourceFile.delete();
        }
        sourceFile.createNewFile();
        File targetFile = new File("D://ft2.txt");
        if(targetFile.exists()){
            targetFile.delete();
        }
        targetFile.createNewFile();
        File ldjFile = new File("NettyDemo/ludingji.txt");
        System.out.println(ldjFile.getAbsolutePath()+";"+ldjFile.length());
        FileOutputStream sourceOutputStream = null;
        FileInputStream sourceInputStream = null;
        FileOutputStream targetOutputStream = null;
        FileInputStream ldjFileInputStream = null;
        try {
            //1、从文件流获取到FileChannel
            sourceOutputStream = new FileOutputStream(sourceFile);
            final FileChannel sourceOutputStreamChannel = sourceOutputStream.getChannel();
            //将数据写入到文件中
            sourceOutputStreamChannel.write(ByteBuffer.wrap(message.getBytes()));
            System.out.println("====1、写入文件完成=====");
            //2、文件拷贝
            sourceInputStream = new FileInputStream(targetFile);
            final FileChannel sourceInputStreamChannel = sourceInputStream.getChannel();

            ByteBuffer buffer = ByteBuffer.allocate((int) sourceFile.length());
            sourceInputStreamChannel.read(buffer);
            //buffer读写切换
            buffer.flip();
            targetOutputStream = new FileOutputStream(targetFile);
            final FileChannel targetOutputChannel = targetOutputStream.getChannel();
            targetOutputChannel.write(buffer);
            System.out.println("=====2、拷贝文件完成 ====");
            //3、读取大文件
            ldjFileInputStream = new FileInputStream(ldjFile);
            final FileChannel ldjChannel = ldjFileInputStream.getChannel();
            ByteBuffer buffer1 = ByteBuffer.allocate(1024);
            //这里就不用while(true)的方式全部读完了。 只读一点点算了。
            for (int i = 0; i < 10; i++) {
                buffer1.clear();
                final int len = ldjChannel.read(buffer1);
                if (len == -1) {
                    break;
                }
                buffer1.flip();
                //这里能看到一些不规则的符号，就是因为fileChannel是按照字节流去读的内容。转换成String时，会存在断码的现象。
                System.out.println("读取到鹿鼎记文本内容 =》 " + new String(buffer1.array(), Charset.forName("utf-8")));
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (null != sourceOutputStream) {
                sourceOutputStream.close();
            }
            if (null != sourceInputStream) {
                sourceInputStream.close();
            }
            if (null != targetOutputStream) {
                targetOutputStream.close();
            }
            if (null != ldjFileInputStream) {
                ldjFileInputStream.close();
            }
        }
    }
}
